<!--TODO 已完成 （查看）-->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="收货人" prop="spUserName">
        <el-input
          v-model="queryParams.spUserName"
          placeholder="请输入收货人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="收货电话" prop="spPhone">
        <el-input
          v-model="queryParams.spPhone"
          placeholder="请输入收货人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="订单编号" prop="spOrderSn">
        <el-input
          v-model="queryParams.spOrderSn"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="订单时间" prop="createTime">
        <el-date-picker clearable
                        v-model="queryParams.createTime"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="请选择订单创建时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          style="background-color: lightpink;color: #FFFFFF"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:record:remove']"
        >删除</el-button>
        <el-button
          style="background-color: #1ab394;color: #FFFFFF"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['shop:order:export']"
        >导出</el-button>
      </el-form-item>
    </el-form>

    订单总量:<span style="color: #1c84c6">{{total}}</span>
    <el-table v-loading="loading" :data="ByComplete" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="spOrderSn" />
      <el-table-column label="商品名称" align="center" prop="spActiveName"/>

      <el-table-column label="收货人" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.spUserName }}</span>
          <br/>
          <span>{{ scope.row.spPhone }}</span>
        </template>
      </el-table-column>

      <el-table-column label="商品图片" align="center" prop="spImage">
        <template slot-scope="scope">
          <image-preview :src="scope.row.spImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="总金额" align="center" prop="spAllMoney" width="60px"/>
      <el-table-column label="应付金额" align="center" prop="spPayMoney" width="80px"/>
      <el-table-column label="订单数量" align="center" prop="spOrderNum" width="80px"/>
      <el-table-column label="配送方式" align="center" prop="spDeliveryWay" width="80px">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.shop_delivery_way" :value="scope.row.spDeliveryWay"/>
        </template>
      </el-table-column>
      <el-table-column label="支付状态" align="center" prop="spPayStatus" width="80px">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.shop_pay_status" :value="scope.row.spPayStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="spOrderStatus" width="80px">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.shop_order_status" :value="scope.row.spOrderStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="warning"
            icon="el-icon-edit"
            @click="handleLook(scope.row)"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      @pagination="ByComplete"/>

<!--    ----------------------------------订单详情信息---------------------------------->
    <el-dialog :visible.sync="openComplete" width="1200px" append-to-body>
      <h2 style="text-align: center">订单信息</h2>
      <el-table :data="orderByComplete" style="width: 86.5%;margin-left: 100px">
        <el-table-column label="订单编号" prop="spOrderSn" width="200"></el-table-column>
        <el-table-column label="电话" prop="spPhone" width="200"></el-table-column>
        <el-table-column label="订单总金额" prop="spAllMoney" width="200"></el-table-column>
        <el-table-column label="应付金额" prop="spPayMoney" width="200"></el-table-column>
        <el-table-column label="订单状态" prop="spOrderStatus" width="200">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.shop_order_status" :value="scope.row.spOrderStatus"/>
          </template>
        </el-table-column>
      </el-table>

      <h2 style="text-align: center">收货信息</h2>
      <!-- 这里可以添加更多的表格或信息展示 -->
      <el-table :data="orderByComplete" style="width: 86.5%;margin-left: 100px">
        <el-table-column label="收货人" prop="spUserName" width="200"></el-table-column>
        <el-table-column label="电话" prop="spPhone" width="200"></el-table-column>
        <el-table-column label="地址" prop="spAddress" width="200"></el-table-column>
        <el-table-column label="支付状态" prop="spPayMoney" width="100"></el-table-column>
        <el-table-column label="配送方式" prop="spDeliveryWay" width="100">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.shop_delivery_way" :value="scope.row.spDeliveryWay"/>
          </template>
        </el-table-column>
        <el-table-column width="200">
          <el-button type="warning" size="mini">修改订单收货信息</el-button>
        </el-table-column>
      </el-table>

      <h2 style="text-align: center">商品信息</h2>
      <!-- 这里可以添加更多的表格或信息展示 -->
      <el-table :data="orderByComplete" style="width: 86.5%;margin-left: 100px">
        <el-table-column label="商品标题" prop="spActiveName" width="200"></el-table-column>
        <el-table-column label="商品图片" prop="spImage" width="200">
          <template slot-scope="scope">
            <image-preview :src="scope.row.spImage" :width="50" :height="50"/>
          </template>
        </el-table-column>
        <el-table-column label="单品价格" prop="spSinglePrice" width="200"></el-table-column>
        <el-table-column label="订单数量" prop="spOrderNum" width="200"></el-table-column>
        <el-table-column label="商品规格" prop="spNormsName" width="200"></el-table-column>
        <el-table-column width="200">
          <el-button type="warning" size="mini">修改订单收货信息</el-button>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">关闭</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  listOrder, getOrderByOrderStatus, getOrderDetailByOrderId, selectDetailDto
} from '@/api/shop/order'

export default {
  name: "Order",
  dicts: ['shop_order_status','shop_pay_status','shop_delivery_way'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品订单列表表格数据
      ByComplete: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        spOrderSn: null,//订单号
        spUserName: null, //收货人
        spPhone: null, //联系方式
        createTime: null, //订单创建时间
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      //发货状态: 已完成
      orderStatus:"Complete",
      openComplete:false,
      orderByComplete:[]
    };
  },
  created() {
    this.ByOrderComplete();
  },
  methods: {
    //查询订单详情
    ByOrderComplete(queryParams) {
      this.loading = true
      if (!queryParams || Object.keys(queryParams).every(key => !queryParams[key] || queryParams[key] === "")){
        getOrderByOrderStatus(this.orderStatus).then(res => {
          this.ByComplete = res.rows
          this.total = res.rows.length
          this.loading = false
      }).catch(error=>{
        console.error('获取关于待发货的订单详情失败:',error)})
      }else{
        //如果查询的参数不为0,则执行下面这个方法
        selectDetailDto(queryParams).then(res=>{
          console.log('根据条件查询的订单详情....',res)
          this.orderDetails = res.rows
          this.total = Array.isArray(res)?res.rows.length : 1
        }).catch(error=>{
          console.error('根据条件查询订单详情失败',error)
        })
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        spOrderSn: null,
        spUserId: null,
        spActiveId: null,
        spDeliveryId: null,
        spAllMoney: null,
        spPayMoney: null,
        spOrderStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    //查看按钮
    handleLook(row){
      this.currentLook = row
      this.openComplete = true

      getOrderDetailByOrderId(this.currentLook.id).then(res=>{
        this.orderByComplete = res.rows
      })

    },

    //关闭查看按钮的弹窗
    closeDialog(){
      this.openComplete = false
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品订单列表编号为"' + ids + '"的数据项？').then(function() {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('shop/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    },
    handleClose() {
      this.dialogVisible = false;
    },
  }
};
</script>
